package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.sort.BaseSort;
import com.file.system.entity.system.WebRole;
import com.file.system.mapper.system.WebRoleMapper;
import com.file.system.service.system.IWebRoleService;
import com.file.system.tools.page.DP;
import com.file.system.tools.sort.SortUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 角色 实现
 *
 * @author jch
 * @since 2022-08-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebRoleServiceImpl implements IWebRoleService {

    @Autowired
    private WebRoleMapper mapper;

    @Autowired
    private SortUtil sortUtil;


    //添加角色
    public boolean save(WebRole webRole) {
        webRole.setId(IdUtil.getSnowflakeNextId());
        //排序
        webRole = sortUtil.add(webRole, webRole.getClass(), "web_role",
                BaseSort.eq().val("is_del", "0")
        );
        if (ObjectUtil.isNull(webRole)) {
            return false;
        }
        return SqlHelper.retBool(mapper.save(webRole));
    }


    //删除角色
    public boolean removeById(@Param("id") Long id) {
        //排序
        WebRole webRole = mapper.selectById(id);
        sortUtil.del(webRole, webRole.getClass(), "web_role",
                BaseSort.eq().val("is_del", "0")
        );
        return SqlHelper.retBool(mapper.removeById(new WebRole().setId(id)));
    }

    //编辑角色
    public boolean updateById(@Param("et") WebRole webRole) {
        if (ObjectUtil.isEmpty(webRole.getId())) {
            return false;
        }
        //查询
        WebRole webRoleOld = mapper.selectById(webRole.getId());
        //排序
        webRole = sortUtil.update(webRole, webRole.getClass(), webRoleOld.getSort(), "web_role",
                BaseSort.eq().val("is_del", "0")
        );
        if (ObjectUtil.isNull(webRole)) {
            return false;
        }
        return SqlHelper.retBool(mapper.updateById(webRole));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<WebRole> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //添加角色(批量)
    public boolean saveBatch(@Param("list") List<WebRole> webRoleList) {
        if (webRoleList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(webRoleList));
    }

    //查询角色
    @DS("slave")
    public List<WebRole> list(@Param("ew") QueryWrapper<WebRole> query) {
        return mapper.select(query);
    }

    //通过ids查询角色集合
    @DS("slave")
    public List<WebRole> listByIds(List<Long> ids) {
        QueryWrapper<WebRole> query = new QueryWrapper<>();
        query.in("a.id", ids);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询所有非删除状态角色
    @DS("slave")
    public List<WebRole> listAll() {
        QueryWrapper<WebRole> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询角色分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebRole> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebRole> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebRole> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询角色
    @DS("slave")
    public WebRole getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询角色
    @DS("slave")
    public WebRole getOne(@Param("ew") QueryWrapper<WebRole> query) {
        List<WebRole> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询角色数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebRole> query) {
        return mapper.selectCount(query);
    }
}
